package cn.edu.swu.myapplication2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.Toast
import androidx.appcompat.app.AlertDialog

class MainActivity : AppCompatActivity(), View.OnClickListener {
    companion object {
        var picFlag:Boolean = true
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val button1: Button = findViewById(R.id.bt1)
        button1.setOnClickListener(this)
        val button2: Button = findViewById(R.id.bt2)
        button2.setOnClickListener(this)
        val button3: Button = findViewById(R.id.bt3)
        button3.setOnClickListener(this)
        val button4: Button = findViewById(R.id.bt4)
        button4.setOnClickListener(this)
//        button1.setOnClickListener {
//            Log.d("MainActivity", "click button")
//        }

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.bt1 -> {
                val et1: EditText = findViewById(R.id.editText)
                val inputText = et1.text.toString()
                Log.d("MainActivity", "click button")
                print(inputText)
                Toast.makeText(this, inputText, Toast.LENGTH_SHORT).show()
            }
            R.id.bt2 -> {
                val iv2: ImageView = findViewById(R.id.imageView)
                if (picFlag) {
                    iv2.setImageResource(R.drawable.img2)
                    picFlag = !picFlag
                } else {
                    iv2.setImageResource(R.drawable.img1)
                    picFlag = !picFlag
                }
            }
            R.id.bt3 -> {
                val progressBar: ProgressBar = findViewById(R.id.progressBar)
                if (progressBar.visibility == View.VISIBLE) {
                    progressBar.visibility = View.GONE
                } else {
                    progressBar.visibility = View.VISIBLE
                }
            }
            R.id.bt4 -> {
                AlertDialog.Builder(this).apply {
                    setTitle("This is Dialog")
                    setMessage("Something important.")
                    setCancelable(false)
                    setPositiveButton("OK") { dialog, which ->
                    }
                    setNegativeButton("Cancel") { dialog, which ->
                    }
                    show()
                }
            }
        }
    }
}